package com.alibaba.travel.scenic.service.impl;

import com.alibaba.travel.common.utils.*;
import com.alibaba.travel.scenic.dao.ScenicSpotDao;
import com.alibaba.travel.scenic.dto.SpotCondition;
import com.alibaba.travel.scenic.entity.ScenicSpotEntity;
import com.alibaba.travel.scenic.service.ScenicSpotService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.alibaba.travel.common.utils.Query;
import com.alibaba.travel.common.utils.PageUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;


@Service("scenicSpotService")
public class ScenicSpotServiceImpl extends ServiceImpl<ScenicSpotDao, ScenicSpotEntity> implements ScenicSpotService {


    /**
     * 根据条件分页查询
     * @param params
     * @param spotCondition
     * @return
     */
    @Override
    public R queryPage(Map<String, Object> params, SpotCondition spotCondition) {
        if(ObjectUtils.isEmpty(params)){
            return R.error("参数为空！");
        }
        Integer page = (Integer) params.get("page");
        Integer pageSize = (Integer) params.get("pageSize");
        Page<ScenicSpotEntity> targetPage = new Page<ScenicSpotEntity>(page, pageSize);
        LambdaQueryWrapper<ScenicSpotEntity> wrapper = new LambdaQueryWrapper<>();
        //条件封装
        String name = spotCondition.getName();
        Float priceLow = spotCondition.getPriceLow();
        Float priceMax = spotCondition.getPriceMax();
        wrapper.like(!StringUtils.isEmpty(name),ScenicSpotEntity::getName,name);
        if(!ObjectUtils.isEmpty(priceLow)){
            wrapper.gt(ScenicSpotEntity::getPrice,priceLow);
        }
        if(!ObjectUtils.isEmpty(priceMax)){
            wrapper.gt(ScenicSpotEntity::getPrice,priceMax);
        }
        //根据点赞数降序排序，根据评论数降序排序
        wrapper.orderByDesc(ScenicSpotEntity::getLikesNumber).orderByDesc(ScenicSpotEntity::getCommentNumber);
        baseMapper.selectPage(targetPage,wrapper);

        return R.ok("查询成功!").setData(targetPage);
    }
}